module datahadler
implicit none
contains
subroutine loadmat(matdat,matdim,ident,fil,col,filename,unitfile)
character(len=15),intent(in)::filename
integer,intent(in)::unitfile,fil,col
double precision, intent(out):: matdat(:,:)
integer, intent(out):: matdim(:,:)
character(len=8),intent(out)::ident(1:fil)
integer i
open(unit=unitfile,file=filename,status="old",&
& action="read")
do i=1,fil,1
	read(unit=unitfile,fmt=*) ident(i),matdim(i,:),matdat(i,:)
    !print*, ident(i),matdim(i,:)
end do
close(unitfile)
end subroutine

subroutine loadmat2(matdat,matdim,fil,col,filename,unitfile)
character(len=7),intent(in)::filename
integer,intent(in)::unitfile,fil,col
double precision, intent(out):: matdat(:,:)
integer, intent(out):: matdim(:,:)
integer i
open(unit=unitfile,file=filename,status="old",&
& action="read")
do i=1,fil,1
	read(unit=unitfile,fmt=*) matdim(i,:),matdat(i,:)
end do
close(unitfile)
end subroutine


subroutine loadmatv(matdat,matdim,ident,fil,col,filename,unitfile)
character(len=15),intent(in)::filename
integer,intent(in)::unitfile,fil,col
double precision, intent(out):: matdat(:,:)
integer, intent(out):: matdim(:,:)
character(len=8),intent(out)::ident(1:fil)
integer i
open(unit=unitfile,file=filename,status="old",&
& action="read")
read(unit=unitfile,fmt=*) ident
read(unit=unitfile,fmt=*) matdim(:,1)
read(unit=unitfile,fmt=*) matdim(:,2)
read(unit=unitfile,fmt=*) matdim(:,3)
do i=1,col,1
	read(unit=unitfile,fmt=*) matdat(:,i)
end do
close(unitfile)
end subroutine


subroutine readmat(matdat,fil,col,filename,unitfile)
character(len=15),intent(in)::filename
integer,intent(in)::unitfile,fil,col
double precision, intent(out):: matdat(1:fil,1:col)
integer i
open(unit=unitfile,file=filename,status="old",&
& action="read")
do i=1,fil,1
	read(unit=unitfile,fmt=*) matdat(i,:)
end do
close(unitfile)
end subroutine

subroutine readvec(vec,dim,filename,unitfile)
character(len=13),intent(in)::filename
integer,intent(in)::unitfile,dim
double precision, intent(out):: vec(dim)
integer i

open(unit=unitfile,file=filename,status="old",&
& action="read")

do i=1,dim,1
	read(unit=unitfile,fmt=*) vec(i)
end do
close(unitfile)
end subroutine

subroutine readvecint(vec,dim,filename,unitfile)
character(len=13),intent(in)::filename
integer,intent(in)::unitfile,dim
integer, intent(out):: vec(dim)
integer i

open(unit=unitfile,file=filename,status="old",&
& action="read")

do i=1,dim,1
	read(unit=unitfile,fmt=*) vec(i)
end do
close(unitfile)
end subroutine


subroutine readrow(rowcontent,rownum,dimfil,filename,unitfile)
integer, intent(in):: rownum,dimfil,unitfile
character(len=15),intent(in)::filename
double precision, intent(out):: rowcontent(1:dimfil)
integer i

OPEN(unit=unitfile,file=filename,status="old",&
& action="read")

do i=1,rownum,1
READ(unit=unitfile,fmt=*) rowcontent(:)
end do
CLOSE(unitfile)

end subroutine readrow
!!!!

subroutine exportmat(matx,maty,file)
double precision, intent(in):: matx(:),maty(:)
character(len=15), intent(in):: file 
integer n,i

n=size(matx)

OPEN(1,file=file,status="replace",&
& action="write")

do i=1,n,1
write(1,fmt=*) matx(i),maty(i)
end do
close(1)
end subroutine exportmat

subroutine exportrmat(matx,file,unit)
double precision, intent(in):: matx(:,:)
character(len=12), intent(in):: file 
integer n,i,unit

n=size(matx,1)

OPEN(unit,file=file,status="replace",&
& action="write")

do i=1,n,1
write(unit,fmt=*) real(matx(i,:))
end do
close(unit)
end subroutine exportrmat


subroutine exportvec(matx,filename,unitind)
double precision, intent(in):: matx(:)
character(len=13), intent(in):: filename 
integer, intent(in)::unitind
integer n,i

n=size(matx)


OPEN(unitind,file=filename,status="replace",&
& action="write")



do i=1,n,1
write(unitind,fmt=*) matx(i)
end do
close(unitind)
end subroutine exportvec

subroutine exportvecint(matx,filename,unitind)
integer, intent(in):: matx(:)
character(len=13), intent(in):: filename 
integer, intent(in)::unitind
integer n,i

n=size(matx)


OPEN(unitind,file=filename,status="replace",&
& action="write")



do i=1,n,1
write(unitind,fmt=*) matx(i)
end do
close(unitind)
end subroutine exportvecint

end module datahadler